//
//  YKDataPersonViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/11/6.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKDataPersonViewController.h"
#import "YKSaleModel.h"
#import <NSDate+TFCore.h>
#import "YKSearchDataByUserViewController.h"
@import Charts;
@interface YKDataPersonViewController ()
@property (nonatomic, strong)NSArray *userSummary;
@property (nonatomic, strong)BarChartView *barChartView;
@end

@implementation YKDataPersonViewController

- (void)loadData {
    [self tf_showStateView:kTFViewStateLoading];
    [TFHTTPMANAER POST:@"manage/userSummary.htm" parameters:@{@"startDate":[NSString stringWithFormat:@"%zd-01-01",[[NSDate date]tf_year]],@"endDate":[[NSDate date] tf_dateStringWithFormat:@"yyyy-MM-dd"]} success:^(id responseObject) {
        self.userSummary = [NSArray yy_modelArrayWithClass:[YKSaleModel class] json:responseObject[@"userSummary"]];
        self.userSummary = self.userSummary.count > 10 ? [self.userSummary subarrayWithRange:NSMakeRange(0, 10)] : self.userSummary;
        [self.barChartView removeFromSuperview];
        [self tf_showStateView:kTFViewStateNone];
        
        self.barChartView = [[BarChartView alloc]initWithFrame:self.view.frame];
        
        [self.view addSubview:self.barChartView];
        
        [self.barChartView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.trailing.equalTo(self.view);
            make.top.equalTo(self.view.mas_top).offset(30);
            make.bottom.equalTo(self.view.mas_bottom).offset(-50);
        }];
        self.barChartView.noDataText = @"暂无数据";//没有数据时的文字提示
        
        
        
        
        self.barChartView.chartDescription.enabled = NO;
        self.barChartView.drawGridBackgroundEnabled = NO;
        self.barChartView.dragEnabled = NO;
        [self.barChartView setScaleEnabled:NO];
        self.barChartView.pinchZoomEnabled = NO;
        self.barChartView.doubleTapToZoomEnabled = NO;
        
        self.barChartView.xAxis.labelPosition = XAxisLabelPositionBottom;
        self.barChartView.rightAxis.enabled = NO;
        
        self.barChartView.drawBarShadowEnabled = NO;
        self.barChartView.drawValueAboveBarEnabled = YES;
        self.barChartView.maxVisibleCount = 60;
        
        ChartXAxis *xAxis = self.barChartView.xAxis;
        xAxis.labelPosition = XAxisLabelPositionBottom;
        xAxis.labelFont = [UIFont systemFontOfSize:10.f];
        xAxis.drawAxisLineEnabled = YES;
        xAxis.drawGridLinesEnabled = NO;
        xAxis.labelRotationAngle = -60;
        
        xAxis.granularity = 1;
        xAxis.granularityEnabled = YES;
        xAxis.decimals = 10;
        xAxis.labelCount = 10;
        xAxis.valueFormatter = self;
        
        ChartYAxis *leftAxis = self.barChartView.leftAxis;
        leftAxis.labelFont = [UIFont systemFontOfSize:10.f];
        leftAxis.drawAxisLineEnabled = YES;
        leftAxis.drawGridLinesEnabled = YES;
        leftAxis.axisMinimum = 0.0; // this replaces startAtZero = YES
        leftAxis.labelPosition = YAxisLabelPositionOutsideChart;//label位置
        
        ChartYAxis *rightAxis = self.barChartView.rightAxis;
        rightAxis.enabled = NO;
        
        
        ChartLegend *l = self.barChartView.legend;
        l.horizontalAlignment = ChartLegendHorizontalAlignmentLeft;
        l.verticalAlignment = ChartLegendVerticalAlignmentBottom;
        l.orientation = ChartLegendOrientationHorizontal;
        l.drawInside = NO;
        l.form = ChartLegendFormSquare;
        l.formSize = 8.0;
        l.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:11.f];
        l.xEntrySpace = 4.0;
        
        self.barChartView.fitBars = YES;
        
        //        double barWidth = 2;
        double spaceForBar = 1;
        
        NSMutableArray *yVals = [[NSMutableArray alloc] init];
        
        for (int i = 0; i < 10; i++)
        {
            if (self.userSummary.count > i) {
                YKSaleModel *model = self.userSummary[i];
                BarChartDataEntry *entry = [[BarChartDataEntry alloc] initWithX:i * spaceForBar y:model.SALES];
                [yVals addObject:entry];
            }
            else {
                BarChartDataEntry *entry = [[BarChartDataEntry alloc] initWithX:i * spaceForBar y:0];
                [yVals addObject:entry];
            }
            
        }
        BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithValues:yVals label:@"销售额(万元)"];
        set1.drawIconsEnabled = NO;
        set1.valueFormatter = self;
        NSMutableArray *dataSets = [[NSMutableArray alloc] init];
        [dataSets addObject:set1];
        
        BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];
        [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];
        
        //        data.barWidth = barWidth;
        
        self.barChartView.data = data;
        
        
        
        //        confirmButton.layer.cornerRadius = 5;
        
    } failure:^(id responseObject, NSError *error) {
        [self tf_showStateView:kTFViewStateNetError];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIButton *confirmButton = [[UIButton alloc]init];
    [confirmButton setTitle:@"查询明细" forState:UIControlStateNormal];
    [confirmButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
    [confirmButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    [self.view addSubview:confirmButton];
    [confirmButton addTarget:self action:@selector(onSearchDetailTapped:) forControlEvents:UIControlEventTouchUpInside];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.trailing.equalTo(self.view);
        make.height.mas_equalTo(40);
    }];
    confirmButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.text = @"本年度品种销量统计表（前十）";
    [self.view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(8);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    titleLabel.font = [UIFont boldSystemFontOfSize:16];
    
    [self loadData];
}
- (NSString *)stringForValue:(double)value
                       entry:(ChartDataEntry *)entry
                dataSetIndex:(NSInteger)dataSetIndex
             viewPortHandler:(id)viewPortHandler{
    return [NSString stringWithFormat:@"%.2f", value];
}
- (void)onSearchDetailTapped:(id)sender {
    YKSearchDataByUserViewController *vc = [[YKSearchDataByUserViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (NSString *)stringForValue:(double)value axis:(ChartAxisBase *)axis {
    
    if (self.userSummary.count > value) {
        YKSaleModel *model = self.userSummary[(int)value];
        return model.USER_NAME;
    }
    return @"";
}

@end
